<?php

/**
 * pm actions.
 *
 * @package    s-test
 * @subpackage pm
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class pmActions extends sfActions
{
  public function executeKnockout(sfWebRequest $request){
      
  }
  public function executeIndex(sfWebRequest $request)
  {
    //Requêtes Ajax
    if($request->isXmlHttpRequest()){
       $arr = array();
       if($request->getParameter('folder')  == "Reçus"){
           $this->pms = KTDPrivateMessage::getNewPlayerPMWithReceiver($this->getUser()->getId());
       }
       else if($request->getParameter('folder')  == "Archive"){
           $this->pms = KTDPrivateMessage::getArchivedPlayerPMWithReceiver($this->getUser()->getId());
       }
       else if($request->getParameter('folder')  == "Envoy%C3%A9s" || $request->getParameter('folder')  == "Envoyés"){
           $this->pms = KTDPrivateMessage::getSentPM($this->getUser()->getId());
       }
       else
           $this->pms = KTDPrivateMessage::getPlayerPMWithReceiver($this->getUser()->getId());
           if(count($this->pms) > 0){
               foreach($this->pms as $i => $pm){

                   $arr[] = array(
                                  
                                  'id'          => $pm['pm']->getId(),
                                  'receivers'   => $pm['pm']->getPseudoReceivers(),
                                  'folder'      => $request->getParameter('folder'),
                                  'from_link'   => $this->generateUrl("show_player", $pm['pm']->getKTDFromPlayer()),
                                  'from_avatar' => $pm['pm']->getKTDFromPlayer()->getAvatar(),
                                  'from_pseudo' => $pm['pm']->getKTDFromPlayer()->getPseudo(),
                                  'delete_link' => $this->generateUrl("delete_pm", $pm['pm']),
                                  'answer_link' => $this->generateUrl("new_pm", array("id" => $pm['pm']->getId())),

                                  'subject'     => $pm['pm']->getTitle(),
                                  'message'     => $pm['pm']->getParsedBody(ESC_RAW),
                                  'date'        => $pm['pm']->getSendedAt(sfConfig::get("default_time_pattern")));
               }
           }
           $json = json_encode($arr);
           $this->getResponse()->setHttpHeader('Content-type', 'application/json');
           return $this->renderText($json);
      
    }
    //Interface normale sans Ajax
    $this->pms = KTDPrivateMessage::getPlayerPMWithReceiver($this->getUser()->getId());
  }

  public function executeShow(sfWebRequest $request)
  {
    //on chope le message
    $this->pm = Doctrine::getTable('KTDPrivateMessage')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->pm);
    $this->sender = $this->pm->getKTDFromPlayer();

    //on chope le receiver
    $this->rc = $this->pm->getReceiverInfo($this->getUser()->getId());
    //on verrouille si le message n'était pas destiné au joueur
    $this->forward404Unless($this->rc, "Vous ne pouvez pas lire ce message privé !");

    $this->rc->setOld();
    $this->rc->save();

  }

  public function executeNew(sfWebRequest $request)
  {
    if($request->getParameter('id')){
        $rpm = Doctrine::getTable('KTDPrivateMessage')->find(array($request->getParameter('id')));
        if($rpm != NULL){
            $r = new KTDPrivateMessage();
            $r->setTitle("Re: ".$rpm->getTitle());
            $r->setBody("<citation>".$rpm->getBody()."</citation>");
            $this->form = new KTDPrivateMessageForm($r);
        }
        else
            $this->form = new KTDPrivateMessageForm();
    }
    else
        $this->form = new KTDPrivateMessageForm();

    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $mp = new KTDPrivateMessage();
    $mp->setKTDFromPlayer($this->getUser()->getGuardUser());
    $this->form = new KTDPrivateMessageForm($mp);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_private_message = Doctrine::getTable('KTDPrivateMessage')->find(array($request->getParameter('id'))), sprintf('Object ktd_private_message does not exist (%s).', $request->getParameter('id')));  
    //on chope le receiver
    $rc = $ktd_private_message->getReceiverInfo($this->getUser()->getId());
    //on verrouille si le message n'était pas destiné au joueur
    $this->forward404Unless($rc, "Vous ne pouvez pas supprimer ce message privé !");
    $ktd_private_message->remove($this->getUser()->getGuardUser());

    $this->redirect('pm/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ktd_private_message = $form->save();
      $this->redirect($this->generateUrl("pm_box"));
    }
  }
}
